import axios from 'axios'

export const getLocalCity = () => {
  const localCity = JSON.parse(localStorage.getItem('local_city'));

  if (localCity) {
    return Promise.resolve(localCity)
  }

  const myCity = new window.BMap.LocalCity();

  return new Promise((resolve, reject) => {
    myCity.get(async result => {
      const cityName = result.name;

      const { data } = await axios.get(`http://127.0.0.1:8080/area/info?name=${cityName}`);

      if (data.status !== 200) {
        reject('错误');
      }

      resolve(data.body);
      localStorage.setItem('local_city', JSON.stringify(data.body));
    });
  })

}